import CryptoJS from "crypto-js";

const SCRIPT_KEY = CryptoJS.enc.Utf8.parse(import.meta.env.VITE_APP_SCRIPT_KEY); //十六位十六进制数作为密钥
const iv = CryptoJS.enc.Utf8.parse(import.meta.env.VITE_APP_SCRIPT_VI);

// Encrypt
const encrypt = (key: string) => {
  const message = CryptoJS.enc.Utf8.parse(key);
  return CryptoJS.AES.encrypt(message, SCRIPT_KEY, {
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
    iv
  }).toString();
};

// Decrypt
const decrypt = (value: string) => {
  return CryptoJS.AES.decrypt(value, SCRIPT_KEY, {
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
    iv
  }).toString(CryptoJS.enc.Utf8);
};

export { encrypt, decrypt };
